package DataStructure;

import java.util.jar.JarException;

import Interface.IStack;

public class MyLinkedStack<T> implements IStack<T> {
	//成员变量
	private int theSize;
	//用链表实现的，top指向第一个元素
	private Node<T> top = new Node<T>();
	//静态内部类
	private static class Node<T>{
		public Node<T> next;
		public T data;
		
		public Node(){
			this(null,null);
		}
		public Node(T data,Node<T> next){
			this.data = data;
			this.next = next;
		}
	}
	public MyLinkedStack(){
		clear();
	}
	@Override
	public T pop() {
		// TODO Auto-generated method stub
		//判断
		if(isEmpty()){
			throw new java.util.EmptyStackException();
		}
		T tmp = top.data;
		top = top.next;
		theSize--;
		return tmp;
	}

	@Override
	public boolean push(T data) {
		// TODO Auto-generated method stub
		Node<T> newNode = new Node<T>(data, null);
		//要判断是不是为空，因为行为是不一样的
		if(isEmpty()){
			top = newNode;
		}else{
			//直接往上面放就行
			newNode.next = top;
			top = newNode;
		}
		theSize++;
		return true;
	}

	@Override
	public T top() {
		// TODO Auto-generated method stub
		return top.data;
	}
	@Override
	public void clear() {
		// TODO Auto-generated method stub
		theSize = 0;
		top = null;
	}
	@Override
	public boolean isEmpty() {
		// TODO Auto-generated method stub
		return theSize==0;
	}

}
